﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PominaScale.DataAccess;

namespace PominaScale
{
    public partial class FrmProducts : Form
    {
        #region Ctors
        private FrmProducts()
        {
            InitializeComponent();

            Initialize();

        }
        #endregion

        #region Singleton Members
        private static volatile FrmProducts instance;
        private static object syncRoot = new Object();

        public static FrmProducts Instance
        {
            get
            {
                lock (syncRoot)
                {
                    if (instance == null)
                        instance = new FrmProducts();
                }

                return instance;
            }
        }

        protected override void OnClosed(EventArgs e)
        {
            instance = null;
            base.OnClosed(e);
        }
        #endregion

        #region Field Members
        IProductDAO _dataAccess = new ProductDAO();
        IList<ProductModel> _productList;
        #endregion

        #region Method Members

        /// <summary>
        /// Initialize methods of the application
        /// </summary>
        private void Initialize()
        {
            // Set Events
            this.btnReset.Click += new EventHandler(delegate { ClickButtonReset(); });
            this.btnSearch.Click += new EventHandler(delegate { ClickButtonSearch(); });
            this.btnNew.Click += new EventHandler(delegate { ClickButtonNew(); });
            this.btnEdit.Click += new EventHandler(delegate { ClickButtonEdit(); });
            this.btnDelete.Click += new EventHandler(delegate { ClickButtonDelete(); });
            this.btnClose.Click += new EventHandler(delegate { instance = null; this.Close(); });
            this.gridEX1.DoubleClick += new EventHandler(delegate { ClickButtonEdit(); });

            // Get all products
            this.ClickButtonReset();

            // Set permission
            //bool IsAdmin = (Common.UserLogin.GroupPermissionID == 1);
            //this.btnDelete.Visible = IsAdmin;
            //this.btnEdit.Visible = IsAdmin;
        }


        #endregion

        #region Action Members

        /// <summary>
        /// Click button search
        /// </summary>
        private void ClickButtonSearch()
        {
            _dataAccess.SearchObject = new ProductModel();
            _dataAccess.SearchObject.ProductID = this.edtProductID.Text;
            _dataAccess.SearchObject.ProductName = this.edtProductName.Text;
            _dataAccess.SearchObject.Unit = this.edtUnit.Text;

            _productList = _dataAccess.Search();

            if (_productList == null || _productList.Count == 0)
                this.gridEX1.BuiltInTexts[Janus.Windows.GridEX.GridEXBuiltInText.GroupByBoxInfo] = "Không tìm thấy thông tin."; // Bạn phải nhập ít nhất 1 tiêu chí tìm kiếm.";
            else
                this.gridEX1.BuiltInTexts[Janus.Windows.GridEX.GridEXBuiltInText.GroupByBoxInfo] = "Chọn cột cần group.";

            this.gridEX1.DataSource = _productList;
        }

        /// <summary>
        /// Click button reset
        /// </summary>
        private void ClickButtonReset()
        {
            this.edtProductID.Text = String.Empty;
            this.edtProductName.Text = String.Empty;
            this.edtUnit.Text = String.Empty;

            this.ClickButtonSearch();

            this.edtProductID.Focus();
        }

        /// <summary>
        /// Click button new
        /// </summary>
        private void ClickButtonNew()
        {
            int count = _productList.Count;
            // Show the updating staff instance
            FrmUpdateProducts instance = new FrmUpdateProducts();
            instance.Collection = _productList;
            instance.ShowDialog();

            if (count < _productList.Count)
            {
                // Refresh data
                this.gridEX1.Refetch();
            }
        }

        /// <summary>
        /// Click button edit
        /// </summary>
        private void ClickButtonEdit()
        {
            if (this.gridEX1.CurrentRow == null)
            {
                MessageBoxEx.ShowAlert("Vui lòng cho dòng cần chỉnh sửa");
                return;
            }

            // Edit employee information
            FrmUpdateProducts instance = new FrmUpdateProducts();
            instance.Product = this.gridEX1.CurrentRow.DataRow as ProductModel;
            DialogResult result = instance.ShowDialog();
            if (result == DialogResult.OK)
            {
                // Refresh data
                this.gridEX1.Refetch();
            }
        }

        /// <summary>
        /// Click button delete
        /// </summary>
        private void ClickButtonDelete()
        {
            try
            {
                if (this.gridEX1.CurrentRow == null || this.gridEX1.CurrentRow.DataRow == null)
                {
                    MessageBoxEx.ShowAlert("Vui lòng chọn dòng cần xóa!");
                    return;
                }

                ProductModel product = this.gridEX1.CurrentRow.DataRow as ProductModel;
                bool? result = MessageBoxEx.ShowYesNoQuestion("Bạn có chắc chắn muốn xóa thông tin này?");
                if (result.HasValue && result.Value)
                {
                    string productID = product.ProductID;
                    bool r = _dataAccess.Delete(product);

                    if (r)
                    {
                        this.gridEX1.CurrentRow.Delete();
                        this.gridEX1.Refetch();
                    }
                    else
                    {
                        MessageBoxEx.ShowAlert("Quá trình xóa hàng hóa không thành công.");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion

    }
}
